﻿using AutoMapper;
using BS.Domain.traceability;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.Tranceabilty
{
    public class AddTranceCommandHandler : IRequestHandler<InsertTranceShuoYuanCommand, ApiResult>
    {
        private readonly ItraceabilitableRepository _traceabilitableRepository;

        private readonly IMapper _mapper;

        private readonly ILogger<AddTranceCommandHandler> _logger;

        public AddTranceCommandHandler(ItraceabilitableRepository traceabilitableRepository, IMapper mapper, ILogger<AddTranceCommandHandler> logger)
        {
            _traceabilitableRepository = traceabilitableRepository;
            _mapper = mapper;
            _logger = logger;
        }
        public async Task<ApiResult> Handle(InsertTranceShuoYuanCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var trance = _mapper.Map<traceabilitable>(request);
                var result= await _traceabilitableRepository.InsertAsync(trance);
                return await Task.FromResult(ApiResult.Ok("保存数据成功"+result));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "InsertTranceCommandHandler");
                return await Task.FromResult(ApiResult.Fail("保存数据失败"));
            }
        }
    }
}
public class InsertTranceShuoYuanCommand:IRequest<ApiResult>
{
    /// <summary>
    /// 商品外键
    /// </summary>
    public int GoodsId { get; set; }
    /// <summary>
    /// 生产批次
    /// </summary>
    public string Productionbatches { get; set; } = string.Empty;
    /// <summary>
    /// 保质期（天）
    /// </summary>
    public int ShelflifeDay { get; set; }
    /// <summary>
    /// 生产许可证
    /// </summary>
    public string Productionlicense { get; set; } = string.Empty;
    /// <summary>
    /// 生产企业
    /// </summary>
    public string ProductionCompany { get; set; } = string.Empty;
    /// <summary>
    /// 封面图
    /// </summary>
    public string Coverimage { get; set; } = string.Empty;
}